package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Permission;
import com.xmy.cultivate.service.IPermissionService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-08-07
 */
@RestController
@RequestMapping("/api/permission")
@Scope("prototype")
public class PermissionController extends BaseController{

    @Autowired
    IPermissionService iPermissionService;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap){
        String type = (String) reMap.get("type");
        QueryWrapper<Permission> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(type),"type",type);
        List<Permission> permissionList = iPermissionService.list(queryWrapper);
        return new Result(Code.OK,new PageResult(permissionList,(long)permissionList.size(),1L));
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id){
        Permission permission = iPermissionService.getById(id);
        return new Result(Code.OK,permission);
    }


    @PostMapping()
    public Result save(@RequestBody Permission permission){
        //dict.setId(idWorker.nextId());
        boolean rs = iPermissionService.saveTo(permission);
        return new Result(rs?Code.OK:Code.ERROR);
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id,@RequestBody Permission permission){
        boolean rs = iPermissionService.updateTo(permission);
        return new Result(rs?Code.OK:Code.ERROR);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        boolean rs = iPermissionService.deleteById(id);
        return new Result(rs?Code.OK:Code.ERROR);
    }

}
